package StudyCode.summary.BufferAndChannel;

import java.nio.CharBuffer;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class FillAndDrainCharBuffer {
	public static void main(String[] argv) {
		CharBuffer buffer = CharBuffer.allocate(100);

		while (fillBuffer(buffer)) {
			buffer.flip();
			drainBuffer(buffer);
			buffer.clear();
		}
	}

	private static void drainBuffer(CharBuffer buffer) {
		while (buffer.hasRemaining()) {
			System.out.print(buffer.get());
		}
		System.out.println("");
	}

	private static boolean fillBuffer(CharBuffer buffer) {
		if (index >= strings.length) {
			return (false);
		}
		String string = strings[index++];
		for (int i = 0; i < string.length(); i++) {
			buffer.put(string.charAt(i));
		}
		return (true);
	}

	private static int index = 0;

	private static String[] strings = {"1", "2", "3", "4", "5", "6",};
}
